package com.llkj.contractmanagement.contractmanagement.mine;

import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.StringUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/18 18:09
 * description:意见反馈界面
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener, TextWatcher {


    @Bind(R.id.title_feedback)
    TitleBar titleBar;
    @Bind(R.id.rg_gradle)
    RadioGroup radioGroup;
    @Bind(R.id.rb_type1)
    RadioButton rb_type1;//功能意见
    @Bind(R.id.rb_type2)
    RadioButton rb_type2;//界面意见
    @Bind(R.id.rb_type3)
    RadioButton rb_type3;//新需求
    @Bind(R.id.et_feedback_content)
    EditText et_content;//反馈内容
    @Bind(R.id.et_phonenumber)
    EditText et_phonenumber;//联系方式
    @Bind(R.id.tv_input_font_number)
    TextView tv_fontnumber;
    @Bind(R.id.btn_commit)
    Button btn_commit;//提交反馈
    //    phone	1	string	手机号
    private String phone;
    //    type	1	string	类型（1 功能 2界面 3新需求）
    private int type = 1;
    //    user_id	1	string	用户的id
    //    token	1	string	Token
    private UserInfoBean userInfoBean;
    //    content	1	string	反馈内容
    private String content;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        userInfoBean = UserInfoBean.getInstance();
        initView();
        setListener();

    }

    private void initView() {
        String text = "还可输入150字";
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.btn_login_ok)), 4, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
        tv_fontnumber.setText(style);
    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        btn_commit.setOnClickListener(this);
        et_content.addTextChangedListener(this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_type1) {
                    type = 1;
                } else if (checkedId == R.id.rb_type2) {
                    type = 2;
                } else if (checkedId == R.id.rb_type3) {
                    type = 3;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_commit://反馈提交
                feedBack();
                break;
        }
    }

    private void feedBack() {
        phone = et_phonenumber.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.makeShortText(this, "请输入反馈内容");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.makeShortText(this, "请输入反馈电话");
            return;
        }
        if (!StringUtil.isPhoneNumber(phone)) {
            ToastUtil.makeShortText(this, "请输入正确的电话号码");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
//        phone	1	string	手机号
//        type	1	string	类型（1 功能 2界面 3新需求）
//        user_id	1	string	用户的id
//        token	1	string	Token
//        content	1	string	反馈内容
        map.put("phone", phone);
        map.put("type",type+"");
        map.put("user_id", userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        map.put("content", content);
        HttpMethod.feedback(this, this, map, UrlConfig.HTTP_FEEDBACK);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        int fontNumber = 150 - s.length();
        String text = "";
        if (fontNumber > 0 && fontNumber < 10) {
            text = "还可输入" + fontNumber + "字";
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.btn_login_ok)), 4, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
            tv_fontnumber.setText(style);
        } else if (fontNumber > 0 && fontNumber < 100) {
            text = "还可输入" + fontNumber + "字";
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.btn_login_ok)), 4,6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
            tv_fontnumber.setText(style);
        } else if (fontNumber >= 100) {
            text = "还可输入" + fontNumber + "字";
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.btn_login_ok)), 4, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
            tv_fontnumber.setText(style);
        }else if (fontNumber == 0) {
            text = "还可输入0字";
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.btn_login_ok)), 4, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色
            tv_fontnumber.setText(style);
        }

        if (!TextUtils.isEmpty(s)) {
            content = s.toString();
        }
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_FEEDBACK:
                ToastUtil.makeShortText(this, "提交成功");
                this.finish();
                break;
        }
    }
}
